package action;

import java.security.MessageDigest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.ServiceManager;
import service.UsersService;
import form.AdminSettingForm;

public class AdminSettingAction extends ConnectionAwareAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			AdminSettingForm input = (AdminSettingForm) form;  
		
			String pass1 = input.getPass1();
			String pass2 = input.getPass2();
			
			UsersService usersService = ServiceManager.getDefault().getUsersService(); 
			if (pass1 !="" && pass2 !=""){
				
				if (pass1.equals(pass2)){      
					MessageDigest md5 ;        
					StringBuffer  hexString = new StringBuffer();
					md5 = MessageDigest.getInstance("md5");
					md5.reset();
					md5.update(pass1.getBytes());
					byte messageDigest[] = md5.digest();
					for (int i = 0; i < messageDigest.length; i++) {
						hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
					}
					usersService.setUsers(hexString.toString());
				}
				else 
				{
					return mapping.findForward(ForwardActionTo.ERROR);
				}
			}
		return mapping.findForward(ForwardActionTo.SUCCESS);
	}
}
